package com.avcdata.national.activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avcdata.aoscustommade.R;
import com.avcdata.aoscustommade.activity.Login_Activity;
import com.avcdata.aoscustommade.main.BaseActivity;
import com.avcdata.aoscustommade.utils.AvcConstants;
import com.avcdata.aoscustommade.utils.SharedPreferencesUtils;
import com.avcdata.national.adapter.PersonalAddProductAdapter;
import com.avcdata.national.module.AllBrands;
import com.avcdata.national.module.BrandsPageDatakInfo;
import com.avcdata.national.module.TopBrands;
import com.avcdata.national.module.UserBrandsInfo;
import com.avcdata.national.network.BaseLog;
import com.avcdata.national.network.JsonRunnable;
import com.avcdata.national.network.ThreadPoolUtils;
import com.avcdata.national.utils.sortListView.CharacterParser;
import com.avcdata.national.utils.sortListView.PinyinComparator;
import com.avcdata.national.utils.sortListView.SideBar;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * 添加竞品
 *
 * @author Administrator
 */
public class PersonalAddProductActivity extends BaseActivity{
    @ViewInject(R.id.addproduct_sure_btn)
    private Button addproduct_sure_btn;
    @ViewInject(R.id.tv_note)
    private TextView tv;
    @ViewInject(R.id.left_group)
    private LinearLayout left_group;
    @ViewInject(R.id.leftText)
    private TextView leftText;
    @ViewInject(R.id.right_group)
    private LinearLayout right_group;
    @ViewInject(R.id.title)
    private TextView title;

    @ViewInject(R.id.topBrands1)
    private ImageView topBrands1;
    @ViewInject(R.id.topBrands2)
    private ImageView topBrands2;
    @ViewInject(R.id.topBrands3)
    private ImageView topBrands3;
    @ViewInject(R.id.topBrands4)
    private ImageView topBrands4;
    @ViewInject(R.id.topBrands5)
    private ImageView topBrands5;
    @ViewInject(R.id.topBrands6)
    private ImageView topBrands6;
    @ViewInject(R.id.topBrands7)
    private ImageView topBrands7;
    @ViewInject(R.id.topBrands8)
    private ImageView topBrands8;
    @ViewInject(R.id.topBrands9)
    private ImageView topBrands9;
    @ViewInject(R.id.topBrands10)
    private ImageView topBrands10;
    @ViewInject(R.id.topBrands11)
    private ImageView topBrands11;
    @ViewInject(R.id.topBrands12)
    private ImageView topBrands12;
    @ViewInject(R.id.topBrands13)
    private ImageView topBrands13;
    @ViewInject(R.id.topBrands14)
    private ImageView topBrands14;
    @ViewInject(R.id.topBrands15)
    private ImageView topBrands15;
    @ViewInject(R.id.topBrandsAdd1)
    private TextView topBrandsAdd1;
    @ViewInject(R.id.topBrandsAdd2)
    private TextView topBrandsAdd2;
    @ViewInject(R.id.topBrandsAdd3)
    private TextView topBrandsAdd3;
    @ViewInject(R.id.topBrandsAdd4)
    private TextView topBrandsAdd4;
    @ViewInject(R.id.topBrandsAdd5)
    private TextView topBrandsAdd5;
    @ViewInject(R.id.topBrandsAdd6)
    private TextView topBrandsAdd6;
    @ViewInject(R.id.topBrandsAdd7)
    private TextView topBrandsAdd7;
    @ViewInject(R.id.topBrandsAdd8)
    private TextView topBrandsAdd8;
    @ViewInject(R.id.topBrandsAdd9)
    private TextView topBrandsAdd9;
    @ViewInject(R.id.topBrandsAdd10)
    private TextView topBrandsAdd10;
    @ViewInject(R.id.topBrandsAdd11)
    private TextView topBrandsAdd11;
    @ViewInject(R.id.topBrandsAdd12)
    private TextView topBrandsAdd12;
    @ViewInject(R.id.topBrandsAdd13)
    private TextView topBrandsAdd13;
    @ViewInject(R.id.topBrandsAdd14)
    private TextView topBrandsAdd14;
    @ViewInject(R.id.topBrandsAdd15)
    private TextView topBrandsAdd15;
    @ViewInject(R.id.topBrandsGroup1)
    private RelativeLayout topBrandsGroup1;
    @ViewInject(R.id.topBrandsGroup2)
    private RelativeLayout topBrandsGroup2;
    @ViewInject(R.id.topBrandsGroup3)
    private RelativeLayout topBrandsGroup3;
    @ViewInject(R.id.topBrandsGroup4)
    private RelativeLayout topBrandsGroup4;
    @ViewInject(R.id.topBrandsGroup5)
    private RelativeLayout topBrandsGroup5;
    @ViewInject(R.id.topBrandsGroup6)
    private RelativeLayout topBrandsGroup6;
    @ViewInject(R.id.topBrandsGroup7)
    private RelativeLayout topBrandsGroup7;
    @ViewInject(R.id.topBrandsGroup8)
    private RelativeLayout topBrandsGroup8;
    @ViewInject(R.id.topBrandsGroup9)
    private RelativeLayout topBrandsGroup9;
    @ViewInject(R.id.topBrandsGroup10)
    private RelativeLayout topBrandsGroup10;
    @ViewInject(R.id.topBrandsGroup11)
    private RelativeLayout topBrandsGroup11;
    @ViewInject(R.id.topBrandsGroup12)
    private RelativeLayout topBrandsGroup12;
    @ViewInject(R.id.topBrandsGroup13)
    private RelativeLayout topBrandsGroup13;
    @ViewInject(R.id.topBrandsGroup14)
    private RelativeLayout topBrandsGroup14;
    @ViewInject(R.id.topBrandsGroup15)
    private RelativeLayout topBrandsGroup15;

    @ViewInject(R.id.detele_brands1)
    private ImageView detele_brands1;
    @ViewInject(R.id.detele_brands2)
    private ImageView detele_brands2;
    @ViewInject(R.id.detele_brands3)
    private ImageView detele_brands3;
    @ViewInject(R.id.detele_brands4)
    private ImageView detele_brands4;
    @ViewInject(R.id.detele_brands5)
    private ImageView detele_brands5;

    @ViewInject(R.id.selectBrands1)
    private TextView selectBrands1;
    @ViewInject(R.id.selectBrands2)
    private TextView selectBrands2;
    @ViewInject(R.id.selectBrands3)
    private TextView selectBrands3;
    @ViewInject(R.id.selectBrands4)
    private TextView selectBrands4;
    @ViewInject(R.id.selectBrands5)
    private TextView selectBrands5;
    @ViewInject(R.id.selectedBrands1)
    private RelativeLayout selectedBrands1;
    @ViewInject(R.id.selectedBrands2)
    private RelativeLayout selectedBrands2;
    @ViewInject(R.id.selectedBrands3)
    private RelativeLayout selectedBrands3;
    @ViewInject(R.id.selectedBrands4)
    private RelativeLayout selectedBrands4;
    @ViewInject(R.id.selectedBrands5)
    private RelativeLayout selectedBrands5;

    @ViewInject(R.id.hotBrands)
    private LinearLayout hotBrands;

    /**已选择的品牌**/
    private List<String> selectedBrands = new ArrayList<>();
    /**top15竞品**/
    private List<String> top15Brands = new ArrayList<>();
    /**所有竞品**/
    private List<String> allBrands = new ArrayList<>();
    /**用于页面显示的品牌**/
    private List<BrandsPageDatakInfo> pageDatas = new ArrayList<>();

    @ViewInject(R.id.selectedGroup1)
    private LinearLayout selectedGroup1;
    @ViewInject(R.id.selectedGroup2)
    private LinearLayout selectedGroup2;
    private PersonalAddProductAdapter productAdapter;
    @ViewInject(R.id.country_lvcountry)
    private ListView sortListView;
    private SideBar sideBar;
    private TextView dialog;
    private String pageTitle= "";

    @ViewInject(R.id.jiazai_layout)
    private LinearLayout loading;

    private static final String TAG = "看一下距离:";

    /**
     * 汉字转换成拼音的类
     */
    private CharacterParser characterParser;
    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;

    List<TextView> listTv = new ArrayList<TextView> ();
    List<ImageView> listIv = new ArrayList<ImageView> ();
    List<RelativeLayout> listRl = new ArrayList<RelativeLayout> ();
    List<TextView> listSelectedTv = new ArrayList<TextView> ();
    List<ImageView> listSelectIv = new ArrayList<ImageView> ();
    List<RelativeLayout> listSelectedGroup = new ArrayList<RelativeLayout> ();

    private void setViewList() {
        listIv.add ( topBrands1 );
        listIv.add ( topBrands2 );
        listIv.add ( topBrands3 );
        listIv.add ( topBrands4 );
        listIv.add ( topBrands5 );
        listIv.add ( topBrands6 );
        listIv.add ( topBrands7 );
        listIv.add ( topBrands8 );
        listIv.add ( topBrands9 );
        listIv.add ( topBrands10 );
        listIv.add ( topBrands11 );
        listIv.add ( topBrands12 );
        listIv.add ( topBrands13 );
        listIv.add ( topBrands14 );
        listIv.add ( topBrands15 );
        listTv.add ( topBrandsAdd1 );
        listTv.add ( topBrandsAdd2 );
        listTv.add ( topBrandsAdd3 );
        listTv.add ( topBrandsAdd4 );
        listTv.add ( topBrandsAdd5 );
        listTv.add ( topBrandsAdd6 );
        listTv.add ( topBrandsAdd7 );
        listTv.add ( topBrandsAdd8 );
        listTv.add ( topBrandsAdd9 );
        listTv.add ( topBrandsAdd10 );
        listTv.add ( topBrandsAdd11 );
        listTv.add ( topBrandsAdd12 );
        listTv.add ( topBrandsAdd13 );
        listTv.add ( topBrandsAdd14 );
        listTv.add ( topBrandsAdd15 );
        listRl.add ( topBrandsGroup1 );
        listRl.add ( topBrandsGroup2 );
        listRl.add ( topBrandsGroup3 );
        listRl.add ( topBrandsGroup4 );
        listRl.add ( topBrandsGroup5 );
        listRl.add ( topBrandsGroup6 );
        listRl.add ( topBrandsGroup7 );
        listRl.add ( topBrandsGroup8 );
        listRl.add ( topBrandsGroup9 );
        listRl.add ( topBrandsGroup10 );
        listRl.add ( topBrandsGroup11 );
        listRl.add ( topBrandsGroup12 );
        listRl.add ( topBrandsGroup13 );
        listRl.add ( topBrandsGroup14 );
        listRl.add ( topBrandsGroup15 );

        listSelectedTv.add ( selectBrands1 );
        listSelectedTv.add ( selectBrands2 );
        listSelectedTv.add ( selectBrands3 );
        listSelectedTv.add ( selectBrands4 );
        listSelectedTv.add ( selectBrands5 );

        listSelectIv.add ( detele_brands1 );
        listSelectIv.add ( detele_brands2 );
        listSelectIv.add ( detele_brands3 );
        listSelectIv.add ( detele_brands4 );
        listSelectIv.add ( detele_brands5 );

        listSelectedGroup.add ( selectedBrands1 );
        listSelectedGroup.add ( selectedBrands2 );
        listSelectedGroup.add ( selectedBrands3 );
        listSelectedGroup.add ( selectedBrands4 );
        listSelectedGroup.add ( selectedBrands5 );

        for (RelativeLayout rl : listRl) {
            rl.setOnClickListener ( this );
        }

        for (RelativeLayout rl : listSelectedGroup) {
            rl.setOnClickListener ( this );
        }

//        getTop15Brands ();
//        getBrands ();
    }

    @Override
    public void initView() {
        setContentView ( R.layout.activity_personal_addproduct );
        ViewUtils.inject ( this );
        loading.setVisibility(View.GONE);
        addproduct_sure_btn.setOnClickListener ( this );
        //实例化汉字转拼音类
        characterParser = CharacterParser.getInstance ();
        pinyinComparator = new PinyinComparator ();
        sideBar = (SideBar) findViewById ( R.id.sidebar1 );
        dialog = (TextView) findViewById ( R.id.dialog1 );
        sideBar.setTextView ( dialog );
        setViewList ();
    }

    @Override
    public void initInstall() {

    }

    @Override
    public void initData() {
        selectedBrands = getIntent().getStringArrayListExtra("CompetingBrands");
        top15Brands = getIntent().getStringArrayListExtra("Top20Brands");
        allBrands = getIntent().getStringArrayListExtra("OtherBrands");
        pageTitle = getIntent().getStringExtra("categroy");
        tv.setText(pageTitle);
        title.setText(pageTitle);
        leftText.setText("返回品类");
        right_group.setVisibility(View.GONE);

    }

    @Override
    public void evenControl() {
        left_group.setOnClickListener(this);

        productAdapter = new PersonalAddProductAdapter ( PersonalAddProductActivity.this, pageDatas );
        sortListView.setAdapter ( productAdapter );
        sortListView.setOnItemClickListener ( new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
                if (pageDatas.get ( arg2 ).isSelected ()) {
                    pageDatas.get ( arg2 ).setSelected ( !pageDatas.get ( arg2 ).isSelected () );
                    selectedBrands.remove(pageDatas.get(arg2).getBrands());
                } else {
                    if (selectedBrands.size() > 4) {
                        showToast ( "您已选择5个" );
                    } else {
                        pageDatas.get ( arg2 ).setSelected ( !pageDatas.get ( arg2 ).isSelected () );
                        selectedBrands.add(pageDatas.get(arg2).getBrands());
                    }

                }
                productAdapter.notifyDataSetChanged ();
                setSelectBrands ();
                updataTop15 ();

            }
        } );
        //设置右侧触摸监听
        sideBar.setOnTouchingLetterChangedListener ( new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                int position = productAdapter.getPositionForSection ( s.charAt ( 0 ) );
                if (position != -1) {
                    sortListView.setSelection ( position );//如果给listview添加了HeaderView,position相应+1;
                }
            }
        } );
        initSelectBreandsList();
    }

    @Override
    public void onClickEvent(View view) {
        switch (view.getId ()) {
            case R.id.left_group:
                this.finish();
                break;
            case R.id.addproduct_sure_btn:
                getCateSyncInfo();
                break;
            case R.id.topBrandsGroup1:
                addTopBrands ( topBrandsAdd1.getText ().toString (), topBrandsGroup1, topBrands1 );
                break;
            case R.id.topBrandsGroup2:
                addTopBrands ( topBrandsAdd2.getText ().toString (), topBrandsGroup2, topBrands2 );
                break;
            case R.id.topBrandsGroup3:
                addTopBrands ( topBrandsAdd3.getText ().toString (), topBrandsGroup3, topBrands3 );
                break;
            case R.id.topBrandsGroup4:
                addTopBrands ( topBrandsAdd4.getText ().toString (), topBrandsGroup4, topBrands4 );
                break;
            case R.id.topBrandsGroup5:
                addTopBrands ( topBrandsAdd5.getText ().toString (), topBrandsGroup5, topBrands5 );
                break;
            case R.id.topBrandsGroup6:
                addTopBrands ( topBrandsAdd6.getText ().toString (), topBrandsGroup6, topBrands6 );
                break;
            case R.id.topBrandsGroup7:
                addTopBrands ( topBrandsAdd7.getText ().toString (), topBrandsGroup7, topBrands7 );
                break;
            case R.id.topBrandsGroup8:
                addTopBrands ( topBrandsAdd8.getText ().toString (), topBrandsGroup8, topBrands8 );
                break;
            case R.id.topBrandsGroup9:
                addTopBrands ( topBrandsAdd9.getText ().toString (), topBrandsGroup9, topBrands9 );
                break;
            case R.id.topBrandsGroup10:
                addTopBrands ( topBrandsAdd10.getText ().toString (), topBrandsGroup10, topBrands10 );
                break;
            case R.id.topBrandsGroup11:
                addTopBrands ( topBrandsAdd11.getText ().toString (), topBrandsGroup11, topBrands11 );
                break;
            case R.id.topBrandsGroup12:
                addTopBrands ( topBrandsAdd12.getText ().toString (), topBrandsGroup12, topBrands12 );
                break;
            case R.id.topBrandsGroup13:
                addTopBrands ( topBrandsAdd13.getText ().toString (), topBrandsGroup13, topBrands13 );
                break;
            case R.id.topBrandsGroup14:
                addTopBrands ( topBrandsAdd14.getText ().toString (), topBrandsGroup14, topBrands14 );
                break;
            case R.id.topBrandsGroup15:
                addTopBrands ( topBrandsAdd15.getText ().toString (), topBrandsGroup15, topBrands15 );
                break;
            case R.id.selectedBrands1:
                reduceSelectedBrands ( selectBrands1.getText ().toString () );
                break;
            case R.id.selectedBrands2:
                reduceSelectedBrands ( selectBrands2.getText ().toString () );
                break;
            case R.id.selectedBrands3:
                reduceSelectedBrands ( selectBrands3.getText ().toString () );
                break;
            case R.id.selectedBrands4:
                reduceSelectedBrands ( selectBrands4.getText ().toString () );
                break;
            case R.id.selectedBrands5:
                reduceSelectedBrands ( selectBrands5.getText ().toString () );
                break;
            default:
                break;
        }
    }

    /***
     * 初始化已选择品牌的列表
     */
    private void initSelectBreandsList(){
        //筛选缓存中当前选择品类下的品牌
        for (int i = 0; i < allBrands.size (); i++) {
            BrandsPageDatakInfo bpdi = new BrandsPageDatakInfo();
            bpdi.setBrands(allBrands.get(i));
            //汉字转换成拼音
            String pinyin = characterParser.getSelling ( allBrands.get ( i ));
            String sortString = pinyin.substring ( 0, 1 ).toUpperCase ();
            // 正则表达式，判断首字母是否是英文字母
            if (sortString.matches ( "[A-Z]" )) {
                bpdi.setSortLetters ( sortString.toUpperCase () );
            } else {
                bpdi.setSortLetters ( "#" );
            }
            pageDatas.add( bpdi);
        }
        //排序
        Collections.sort ( pageDatas, pinyinComparator );
        productAdapter.notifyDataSetChanged();
        setSelectBrands ();
        setTop15Brands();
        updataTop15();

    }

    /***
     * 初始化top15品牌列表
     */
    private void setTop15Brands() {
        if (top15Brands.size () == 0) {
            hotBrands.setVisibility ( View.GONE );
        } else {
            hotBrands.setVisibility ( View.VISIBLE );
        }
        //
        for (int i = 0; i < (top15Brands.size ()<16?top15Brands.size():15); i++) {
            for (int j = 0; j < selectedBrands.size (); j++) {
                if (top15Brands.get ( i ).equals ( selectedBrands.get (j))) {
                    listRl.get ( i ).setBackgroundResource ( R.drawable.bg_whitecontent_bulestroke );
                    listIv.get ( i ).setBackgroundResource ( R.drawable.product_selected );
                }
            }
            listTv.get ( i ).setText (top15Brands.get ( i ));
        }
    }

    /***
     * 取消关注品牌
     *
     * @param brandName 品牌名称
     */
    private void reduceSelectedBrands(String brandName) {
        for (int i = 0; i < selectedBrands.size (); i++) {
            if (brandName.equals ( selectedBrands.get ( i ) )) {
                Log.e ( "取消关注", "取消关注:" + brandName );
                Log.e ( "取消关注", "resultList.get ( i ).brandName ):" + selectedBrands.get ( i ) );
                selectedBrands.remove ( i );
                Log.e ( "TAG", "这个是删除后数组的个数:" + selectedBrands.size () );
                break;
            }
        }
        for (int j = 0; j < pageDatas.size (); j++) {
            if (pageDatas.get ( j ).getBrands ().equals ( brandName )) {
                pageDatas.get ( j ).setSelected ( false );
                break;
            }
        }
        productAdapter.notifyDataSetChanged ();
        updataTop15 ();
        setSelectBrands ();
    }


    /***
     * 已选竞品的操作
     */
    private void setSelectBrands() {
        Log.e ( "", "缓存的品牌:" + selectedBrands.size () );
        for (int i = 0; i < listSelectedGroup.size (); i++) {
            listSelectedGroup.get ( i ).setVisibility ( View.INVISIBLE );
        }
        switch (selectedBrands.size ()) {
            case 0:
                selectedGroup1.setVisibility ( View.GONE );
                selectedGroup2.setVisibility ( View.GONE );
                break;
            case 1:
                selectedGroup1.setVisibility ( View.VISIBLE );
                selectedGroup2.setVisibility ( View.GONE );
                break;
            case 2:
                selectedGroup1.setVisibility ( View.VISIBLE );
                selectedGroup2.setVisibility ( View.GONE );
                break;
            case 3:
                selectedGroup1.setVisibility ( View.VISIBLE );
                selectedGroup2.setVisibility ( View.GONE );
                break;
            default:
                selectedGroup1.setVisibility ( View.VISIBLE );
                selectedGroup2.setVisibility ( View.VISIBLE );
                break;
        }
        for (int i = 0; i < selectedBrands.size (); i++) {
            if (i < 5) {
                listSelectedTv.get ( i ).setText ( selectedBrands.get ( i ));
                listSelectedGroup.get ( i ).setVisibility ( View.VISIBLE );
            }
        }
    }

    /***
     * 更新TOP15品牌选中状态
     */
    private void updataTop15() {
        for (int i = 0; i < (top15Brands.size ()<16?top15Brands.size():15); i++) {
            listRl.get ( i ).setBackgroundResource ( R.drawable.bg_whitecontent_graystroke );
            listIv.get ( i ).setBackgroundResource ( R.drawable.add_product );
            for (int j = 0; j < selectedBrands.size (); j++) {
                if (top15Brands.get ( i ).equals ( selectedBrands.get ( j ))) {
                    listRl.get ( i ).setBackgroundResource ( R.drawable.bg_whitecontent_bulestroke );
                    listIv.get ( i ).setBackgroundResource ( R.drawable.product_selected );
                }
            }
            listTv.get ( i ).setText ( top15Brands.get ( i ));
        }
    }

    /***
     * 根据TOP15品牌点击统一处理
     *
     * @param BrandName 品牌名称
     */
    private void addTopBrands(String BrandName, RelativeLayout topBrandsGroup, ImageView topBrands) {
        BaseLog.e("aaaa","添加top15");
        if (selectedBrands.size() > 4) {
            showToast ( "当前已选择5个品牌" );
            BaseLog.e("aaaa","当前已选择5个品牌");
        } else {
            if(selectedBrands.contains(BrandName)){

            }else{
                selectedBrands.add ( BrandName );
                BaseLog.e("aaaa","已添加");
                setSelectBrands ();
                updataTop15 ();
            }
        }
    }

    /***
     * 获取页面信息
     */
    private void getCateSyncInfo(){
        HashMap<String,Object> map =new HashMap<>();
        UserBrandsInfo ubi = new UserBrandsInfo();
        ubi.setBrands(selectedBrands);
        ubi.setCategory(pageTitle);
        map.put("userInfo",ubi);
        map.put("lastWeek", new SharedPreferencesUtils(this).getNewWeek());
        ThreadPoolUtils.execute(new JsonRunnable(PersonalAddProductActivity.this,"HTTPPUT", AvcConstants.URLHeaderBase+AvcConstants.URL_CompetingBrand,handler,map,1));
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case -1 :
                    loading.setVisibility(View.GONE);
                    break;
                case 1:
                    loading.setVisibility(View.GONE);
                    PersonalAddProductActivity.this.finish();
                    break;
                case -22:
                    loading.setVisibility(View.GONE);
                    Intent intent = new Intent ();
                    intent.setClass (PersonalAddProductActivity.this, Login_Activity.class );
                    intent.setFlags ( Intent.FLAG_ACTIVITY_CLEAR_TASK );  //注意本行的FLAG设置
                    PersonalAddProductActivity.this.finish ();
                    startActivity ( intent );
                    break;
                default:
                    break;
            }
        }
    };

}